package com.fyj.demo.app.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MySurfaceView extends SurfaceView implements
		SurfaceHolder.Callback {

	private SurfaceHolder holder;
	private MySurfaceViewThread mySurfaceViewThread;
	private boolean hasSurface;

	public MySurfaceView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		init();
	}

	private void init() {
		// create a new SurfaceHolder and assign this class as its callback
		holder = getHolder();
		holder.addCallback(this);
		hasSurface = false;
	}

	public void resume() {
		if (mySurfaceViewThread == null) {
			mySurfaceViewThread = new MySurfaceViewThread();
			if (hasSurface == false)
				mySurfaceViewThread.start();
		}
	}

	public void pause() {
		if (mySurfaceViewThread != null) {
			mySurfaceViewThread.requestExitAndWait();
			mySurfaceViewThread = null;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// TODO Auto-generated method stub
		if (mySurfaceViewThread != null) {
			mySurfaceViewThread.onWindowResize(w, h);
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		hasSurface = true;
		System.out.println("surfaceCreated");
		if (mySurfaceViewThread != null &&  !mySurfaceViewThread.isAlive())
			mySurfaceViewThread.start();
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder arg0) {
		// TODO Auto-generated method stub
		hasSurface = false;
		pause();
	}

	class MySurfaceViewThread extends Thread {
		private boolean done;

		public MySurfaceViewThread() {
			// TODO Auto-generated constructor stub
			super();
			done = false;
		}
		
		

		public void run() {
			SurfaceHolder surfaceHolder = holder;
			// Lock the surface and return the canvas to draw onto
			Canvas canvas = surfaceHolder.lockCanvas();
			while (canvas == null) {
				System.out.println("Waiting cavas to be ready...");
				canvas = surfaceHolder.lockCanvas();
			}
			surfaceHolder.unlockCanvasAndPost(canvas);
			// Repeat the drawing loop until this thread is stopped.
			while (!done) {
				canvas = surfaceHolder.lockCanvas();
				// draw on the canvas
				Paint paint = new Paint();
				paint.setFlags(Paint.ANTI_ALIAS_FLAG);
				paint.setColor(Color.RED);
				canvas.drawText("this is a test", 100, 100, paint);
				// Unlock the canvas and render the current image
				surfaceHolder.unlockCanvasAndPost(canvas);
				System.out.println("drawing...");
			}
		}

		public void requestExitAndWait() {
			// Mark this thread as complete and combine into the main
			// application thread
			done = true;
			try {
				join();
			} catch (InterruptedException ex) {
				ex.printStackTrace();
			}
		}

		public void onWindowResize(int w, int h) {
			// Deal with a change in the available surface size
			// holder.setFixedSize(w, h);
		}
	}

}
